﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VideoCarousel.Control.VlC
{
    /// <summary>
    /// VlcPanleControl.xaml 的交互逻辑
    /// </summary>
    public partial class VlcPanleControl : UserControl,IDisposable,System.ComponentModel.INotifyPropertyChanged
    {
        private System.Timers.Timer timer;
        public event EventHandler<MediaState> StateChanged;
        public event PropertyChangedEventHandler PropertyChanged;
        private Visibility palyVisblillty= Visibility.Visible;
        private Visibility pauseVisblillty = Visibility.Hidden;
        private Visibility videoContorlVisibilty = Visibility.Visible;

        public Visibility PalyVisblillty
        {
            set
            {
                palyVisblillty = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PalyVisblillty"));
            }
            get => palyVisblillty;
        }
        public Visibility PauseVisblillty
        {
            set
            {
                pauseVisblillty = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PauseVisblillty"));
            }
            get => pauseVisblillty;
        }
        public Visibility VideoContorlVisibilty
        {
            set
            {
                videoContorlVisibilty = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("VideoContorlVisibilty"));
            }
            get => videoContorlVisibilty;
        }
        public bool IsShowVideoControl
        {
            set
            {
                if (value)
                    VideoContorlVisibilty = Visibility.Visible;
                else
                    VideoContorlVisibilty = Visibility.Collapsed;
            }
            get
            {
                return VideoContorlVisibilty == Visibility.Visible;
            }
        }
        public VlcPanleControl()
        {
            InitializeComponent();
            _vlc.StateChanged += _vlc_StateChanged;
            _volume.ValueChanged += _volume_ValueChanged;
            Loaded += VideoPlayControl_Loaded;
            Unloaded += VideoPlayControl_Unloaded;
            SetVoumleUI(65);
        }

        private void _volume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            SetVoumleUI(e.NewValue);
        }

        private void SetVoumleUI(double voumle)
        {
            if (voumle == 0)
            {
                SetVoumleLiveUI(0);
            }
            else if (voumle < 35)
            {
                SetVoumleLiveUI(1);
            }
            else if (voumle < 65)
            {
                SetVoumleLiveUI(2);
            }
            else
            {
                SetVoumleLiveUI(3);
            }
        }
        private int tempLive=-1;
        private void SetVoumleLiveUI(int live)
        {
            if (tempLive == live)
                return;
            tempLive = live;
            var on = new SolidColorBrush((System.Windows.Media.Color)ColorConverter.ConvertFromString("#FF2196F3"));
            var off = new SolidColorBrush((System.Windows.Media.Color)ColorConverter.ConvertFromString("#5c5c5c"));
            switch (live)
            {
                case 0:
                    _v1.Fill = off;
                    _v2.Fill = off;
                    _v3.Fill = off;
                    _vp1.Fill = off;
                    _vp2.Fill = off;
                    _vp3.Fill = off;
                    break;
                case 1:
                    _v1.Fill = on;
                    _v2.Fill = off;
                    _v3.Fill = off;
                    _vp1.Fill = on;
                    _vp2.Fill = off;
                    _vp3.Fill = off;
                    break;
                case 2:
                    _v1.Fill = on;
                    _v2.Fill = on;
                    _v3.Fill = off;
                    _vp1.Fill = on;
                    _vp2.Fill = on;
                    _vp3.Fill = off;
                    break;
                case 3:
                    _v1.Fill = on;
                    _v2.Fill = on;
                    _v3.Fill = on;
                    _vp1.Fill = on;
                    _vp2.Fill = on;
                    _vp3.Fill = on;
                    break;
            }
        }

        private void VideoPlayControl_Unloaded(object sender, RoutedEventArgs e)
        {
            timer.Stop();
        }

        private void VideoPlayControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (isDispose)
                return;
            if (timer == null)
            {
                timer = new System.Timers.Timer(500);
                timer.Elapsed += Timer_Elapsed;
            }
            timer.Start();
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (isDispose)
                return;
            if (isDragging)
                return;
            _time.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate {
                if (isDispose)
                    return;
                _time.Value = _vlc.CurrentLength.TotalSeconds;
            });
        }
        public VlC.MediaState MediaState { set; get; }

        private void _vlc_StateChanged(object sender, MediaState e)
        {
            MediaState = e;
            StateChanged?.Invoke(this,e);
            if (e == MediaState.Playing)
            {
                SetPalyControl(VideoPlayPauseEnum.Pause);
            }
            else if (e == MediaState.Stopped)
            {
                SetPalyControl(VideoPlayPauseEnum.Play);
            }
            else if (e == MediaState.Paused)
            {
                SetPalyControl(VideoPlayPauseEnum.Play);
            }
            else 
            {

            }
        }

        internal void Play()
        {
            _vlc.Play();
        }
        internal void Pause()
        {
            _vlc.Pause();
        }
        public void LoadPlay(string fileName)
        {
            _vlc.Load(fileName);
            _vlc.Play();
        }

        private void _paly_Click(object sender, RoutedEventArgs e)
        {
            if (_vlc.MediaState == MediaState.Playing)
            {
                _vlc.Pause();
            }
            else if (_vlc.MediaState == MediaState.Stopped)
            {
                _vlc.Play();

            }
            else if (_vlc.MediaState == MediaState.Paused)
            {
                _vlc.Play();

            }
            else if (_vlc.MediaState == MediaState.NothingSpecial)
            {
                _vlc.Play();

            }
            else if (_vlc.MediaState == MediaState.Opening)
            {
                _vlc.Pause();

            }
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            _vlc.Stop();
        }
        private bool isDragging = false;

        private void _mediaSlider_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isDragging = true;
        }

        private void _mediaSlider_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isDragging = false;
            if (isDispose)
                return;
            _vlc.CurrentLength = TimeSpan.FromSeconds(_time.Value);
        }
        private bool isDispose = false;
        public void Dispose()
        {
            isDispose = true;
            timer.Stop();
            timer.Dispose();
            _vlc.Dispose();
        }

        private void _vlc_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _paly_Click(null, null);
        }
       
        private void SetPalyControl(VideoPlayPauseEnum videoPlayPauseEnum)
        {
            switch (videoPlayPauseEnum)
            {
                case VideoPlayPauseEnum.Play:
                    PalyVisblillty = Visibility.Visible;
                    PauseVisblillty = Visibility.Hidden;
                    break;
                case VideoPlayPauseEnum.Pause:
                    PalyVisblillty = Visibility.Hidden;
                    PauseVisblillty = Visibility.Visible;
                    break;
                default:
                    break;
            }
        }
        private enum VideoPlayPauseEnum
        {
            Play, Pause
        }

        private void _Volume_Click(object sender, RoutedEventArgs e)
        {
            _popup.IsOpen = true;
        }
    }

}
